//connect.c
//gcc -o connect connect.c -I/home/mysql/tc_1.0/source/include -L/home/mysql/tc_1.0/source/lib -lmysqlclient

#include <stdio.h>
#include "mysql.h"
#include <string.h>

int main()
{
  MYSQL mysql;	//need a instance to init
  MYSQL_RES *res;
  MYSQL_ROW row;
  char *query;
  int flag, t;	// connect the database
  mysql_init(&mysql);

  if (!mysql_real_connect(&mysql, "192.168.137.100", "tangchao", "yvhkfhvk", "test", 0, NULL, 0))
  {
    printf("Eror connecting to database: %s\n", mysql_error(&mysql));
  } else
    printf("connected mysql successfully!\n");
  query="select * from tborderdetail order by rand() limit 10"; // sql

  flag=mysql_real_query(&mysql, query, (unsigned int) strlen(query));

  if (flag) // !0,failed
  {
    printf("query failed!\n");
    return 0;
  } else
    printf("[%s] made...\n", query);
  res=mysql_store_result(&mysql);	// store query result

  while (row=mysql_fetch_row(res))
  {
    for (t=0; t < mysql_num_fields(res); t++)
    {
      printf("%s ", row[t]);
    }
    printf("\n");
  }
  mysql_close(&mysql);
  return 0;
}
